<template>
  <div v-if="examData.id">
    <exam-info-view-m timeCount=0 exam-type=6 @sendTipCardClick="tipCardClick"></exam-info-view-m>
    <exam-question-m v-model="selectedIndex" :question-list="questionList" :answer-data="answerData"
                     exam-type=6></exam-question-m>


    <van-popup v-model="showTipCover" position="bottom">
      <tip-card-m v-model="selectedIndex" :question-list="questionList" :answer-data="answerData" exam-type=6 @sendCloseTipCard="closeTipCard"></tip-card-m>
    </van-popup>

  </div>
</template>

<script>

  import TipCardM from '../components/TipCardM'
  import ExamQuestionM from '../components/ExamQuestionM'
  import ExamInfoViewM from '../components/ExamInfoViewM'
  import {Popup} from 'vant'
  import ExamTool from '../ExamTool'

  Vue.use(Popup)

  export default {
    name: 'overall-m',
    //examType 1:统筹考试 2:普通考试 3:模拟考试 4:题库练习 5:错题库 6:题目分析
    data () {
      return {
        examResultId: this.$route.params.examResultId,
        selectedIndex: 0,
        answerData: {},
        questionList: [],
        examData: {},
        showTipCover: false,

      }
    },
    components: {
      TipCardM,
      ExamQuestionM,
      ExamInfoViewM
    },
    mounted () {
      this.loadExamAnalyze()
    },
    methods: {
      loadExamAnalyze () {
        ExamTool.getExamAnalyze(this.examResultId, (data) => {
          this.questionList = data.questions
          this.examData = data.exam

          let answerData = {}
          let logs = data.logs

          //组装作答数据
          logs.forEach((log) => {

            let userResult = JSON.parse(log.userResult) || []
            let answer = {'answer': userResult}

            if (log.examQuestionMainId > 0) {
              //综合题
              let mixId = 'mix' + log.examQuestionMainId
              let mixAnswer = answerData[mixId] || {}
              mixAnswer[log.examQuestionId] = answer
              answerData[mixId] = mixAnswer

            } else {
              //普通题
              answerData[log.examQuestionId] = answer
            }
          })

          this.answerData = answerData

        })
      },
      //打开答题卡
      tipCardClick() {
        this.showTipCover = true
      },
      //关闭答题卡
      closeTipCard() {
        this.showTipCover = false
      },

    }
  }
</script>

